﻿/************************************************
*
* Copyright(c) ZeeGuo.com
*
* CLR : 4.0.30319.296
*
* FileName : DataMatch.cs
*
* Author : Kurodo
*
* CreatTime : 2013/7/4 19:00:03
*           
*************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ComicSite
{
    public static class DataMatch
    {
        public static string RegexValue(string context, string regex, int index)
        {
            string result = string.Empty;
            Regex regeX = new Regex(regex);
            if(regeX.IsMatch(context))
                result = regeX.Match(context).Groups[index].Value;

            return result;
        }

        public static string RegexsValue(string context, string regex, int group)
        {
            string result = string.Empty;
            Regex regeX = new Regex(regex);
            //MatchCollection collect = regeX.Matches(context);
            //foreach (Match mat in collect)
            //{
            //    result = mat.Groups[group].Value;
            //}
            if (regeX.IsMatch(context))
                result = regeX.Matches(context)[0].Groups[group].Value;

            return result;
        }

        public static string RegexsValue(string context, string regex, params int[] group)
        {
            string result = string.Empty;
            Regex regeX = new Regex(regex);

            if (regeX.IsMatch(context))
            {
                MatchCollection collect = regeX.Matches(context);
                foreach (Match mat in collect)
                {
                    result = string.Format("{0} {1}", mat.Groups[group[0]].Value, mat.Groups[group[1]].Value);
                }
            }

            return result;
        }

        public static string RegexsValue(string context, string regex)
        {
            StringBuilder strBuff = new StringBuilder();
            Regex regeX = new Regex(regex);
            if (regeX.IsMatch(context))
            {
                MatchCollection collect = regeX.Matches(context);
                int i = 0;
                foreach (Match mat in collect)
                {
                    i++;
                    string str = i == 1 ? string.Format("{0}", mat.Groups[1].Value) : string.Format(" {0}", mat.Groups[1].Value);
                    strBuff.Append(str);
                }
            }

            return strBuff.ToString();
        }
    }
}
